package cn.mymall.service.impl;

import cn.mymall.entity.User;
import cn.mymall.help.PageHepler;
import cn.mymall.help.PageVo;
import cn.mymall.mapper.UserMapper;
import cn.mymall.service.IUserService;
import cn.mymall.vo.user.UserReqVo;
import cn.mymall.vo.user.UserResVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author liqilun
 * @since 2022-10-22
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<UserMapper, User> implements IUserService {

    @Override
    public User getUserByMobile(String mobile) {
        return getOne(new QueryWrapper<User>().eq("mobile", mobile));
    }

    @Override
    public PageVo queryPage(UserReqVo reqVo) {
        QueryWrapper query = new QueryWrapper<>();
        if (StringUtils.isNotBlank(reqVo.getMobile())) {
            query.eq("mobile", reqVo.getMobile());
        }
        if (StringUtils.isNotBlank(reqVo.getName())) {
            query.like("name", reqVo.getName());
        }
        query.orderByDesc("id");
        Page page = page(new Page(reqVo.getPageNo(), reqVo.getPageSize()), query);
        return PageHepler.convertPageVo(page, UserResVo.class);
    }
}
